package com.binance.basedao.dialect;

/**
 * <b>Description:</b>〈Oracle数据库方言分页〉<br/>
 * @author hongchaoMao <br/>
 *         Create date: 2017/9/18
 * @version v1.0.0
 */
public class OracleDialect extends AbstractDialect
{
    
    @Override
    public String getLimitString(String sql, int offset, int limit)
    {
        sql = sql.trim();
        boolean isForUpdate = false;
        if (sql.toLowerCase().endsWith(" FOR UPDATE")) {
            sql = sql.substring(0, sql.length() - 11);
            isForUpdate = true;
        }
        
        StringBuilder pagingSelect = new StringBuilder(sql.length() + 100);
        
        pagingSelect.append("SELECT * FROM ( SELECT ROW_.*, ROWNUM ROWNUM_ FROM ( ");
        
        pagingSelect.append(sql);
        
        pagingSelect.append(" ) ROW_ ) WHERE ROWNUM_ > " + offset + " AND ROWNUM_ <= " + (offset + limit));
        
        if (isForUpdate) {
            pagingSelect.append(" FOR UPDATE");
        }
        
        return pagingSelect.toString();
    }
    
    @Override
    public String addLog(String sql)
    {
        return null;
    }
    
}
